<?php
	require_once("../../include_dao.php");
	session_start();
	
	if(	$_POST['fkPartner'] != 0 and $_POST['total'] != 0 and  
		$_POST['returndate'] != '' and $_POST['retalsdate'] != '' and 
		isset($_SESSION['RentalCart']) ){
			
			$fkPartner = $_POST['fkPartner'];
			$total = $_POST['total'];
			$returndate = $_POST['returndate'];
			$retalsdate = $_POST['retalsdate'];
			$dtoPartner = unserialize($_SESSION['dtoPartner']);
				
			
			
			// Inicia la Trasaccionalidad para hacer ingresos de informacion
			$transaction = new Transaction();	
			try{
				
				$dtoRental = new Rental();
				
				$dtoRental->fKPartnerEmployee = $dtoPartner->pKPartner;
				$dtoRental->fKPartner = $fkPartner;
				$dtoRental->rentalsDate = $retalsdate;
				$dtoRental->returnDate = $returndate;
				$dtoRental->descunt = 0;
				$dtoRental->totalAmount = $total;
				$dtoRental->active = 1;
				$dtoRental->visible = 1;
				
				/// Guarda La informacion del Partner 
				DAOFactory::getRentalsDAO()->insert($dtoRental);
				
				 foreach ($_SESSION['RentalCart'] as $rentalcart){
					$dtoRentalDetaill = new Rentalsdetail();
					
					$dtoRentalDetaill->fKRentals = $dtoRental->pKRentals;
					$dtoRentalDetaill->fKMovie = $rentalcart["id"];
					$dtoRentalDetaill->comment = $rentalcart["name"];
					$dtoRentalDetaill->qauntity = 1;
					$dtoRentalDetaill->cost = $rentalcart["priceTotal"];
					
					/// Guarda La informacion del Partner 
					DAOFactory::getRentalsdetailDAO()->insert($dtoRentalDetaill);																							
				 }
				
				//commit transaction
				$transaction->commit();
				
				//Se elimina la session del carrito
				unset($_SESSION['RentalCart']);			
				header( 'Location: ../../rentalcart.php?err=40');
			}catch (Exception $e){	
				
					// Anulo la Transaccion
					$transaction->rollback();			
					header( 'Location: ../../rentalcart.php?err=41&msn=$e');
				}	
				
	}else{
		header( 'Location: ../../rentalcart.php?err=41');
	}
?>
